{-# LANGUAGE DerivingStrategies #-}

module Aria2.Status where

import Aria2.Bytes (Bytes)
import Aria2.Error (Aria2Error)
import Aria2.File (FileInfo)
import Aria2.GID (GID)
import Data.Text (Text)
import Data.Time.Clock (UTCTime)
import Data.Vector.NonEmpty (NonEmptyVector)
import Text.URI (URI)

data StatusKey
  = Gid
  | Status
  | TotalLength
  | CompletedLength
  | UploadLength
  | Bitfield
  | DownloadSpeed
  | UploadSpeed
  | InfoHash
  | NumSeeders
  | Seeder
  | PieceLength
  | NumPieces
  | Connections
  | ErrorCode
  | ErrorMessage
  | FollowedBy
  | Following
  | BelongsTo
  | Dir
  | Files
  | BitTorrent
  | VerifiedLength
  | VerifyIntegrityPending
  deriving stock (Eq, Show)

data DownloadStatus = Active | Waiting | Paused | Error | Complete | Removed
  deriving stock (Eq, Show)

data BitTorrentMode = Single | Multi
  deriving stock (Eq, Show)

data BitTorrentInfo = BitTorrentInfo
  { announceList :: {-# UNPACK #-} !(NonEmptyVector URI),
    comment :: {-# UNPACK #-} !Text, -- best guess
    creationDate :: {-# UNPACK #-} !UTCTime,
    mode :: !BitTorrentMode,
    name :: {-# UNPACK #-} !Text -- more best guesses
  }
  deriving stock (Eq, Show)

-- Does not include 'following' (unclear)
data Status = S
  { _gid :: !(Maybe GID),
    _status :: !(Maybe DownloadStatus),
    _totalLength :: !(Maybe Bytes),
    _completedLength :: !(Maybe Bytes),
    _bitfield :: !(Maybe Text), -- best guess
    _downloadSpeed :: !(Maybe Bytes),
    _uploadSpeed :: !(Maybe Bytes),
    _infoHash :: !(Maybe Text), -- again, best guess
    _numSeeders :: !(Maybe Word),
    _seeder :: !(Maybe Bool),
    _pieceLength :: !(Maybe Bytes),
    _numPieces :: !(Maybe Word),
    _connections :: !(Maybe Word),
    _errorCode :: !(Maybe Aria2Error),
    _errorMessage :: !(Maybe Text),
    _followedBy :: !(Maybe (NonEmptyVector GID)),
    _belongsTo :: !(Maybe GID),
    _dir :: !(Maybe Text),
    _files :: !(Maybe (NonEmptyVector FileInfo)),
    _verifiedLength :: !(Maybe Bytes),
    _verifyIntegrityPending :: !(Maybe Bool)
  }
  deriving stock (Eq)
